<!--
 * @Author: your name
 * @Date: 2021-08-06 22:04:29
 * @LastEditTime: 2021-08-13 09:33:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hrsaas-86\src\views\employees\index.vue
-->
<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button
            type="primary"
            size="small"
            @click.native="showDialog = true"
          >
            新增员工
          </el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" :data="list" border stripe>
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            sortable=""
            width="100"
          />
          <el-table-column label="姓名" prop="username" sortable="" />
          <!-- 头像列最后来处理 -->
          <!-- <el-table-column label="头像" prop="staffPhoto" sortable="" /> -->
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
            :formatter="formatter"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <!-- {{ row }} -->

              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 分页组件 -->
      <el-pagination
        style="margin-top: 20px"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <add-employee :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import AddEmployee from './components/add-employee'
import { reqGetEmployeeList, reqDelEmployee } from '@/api/employees'
import employeeMenu from '@/constant/employees'

export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      page: 1,
      size: 5,
      list: [], // 员工列表
      total: 0, // 员工总数
      loading: false,
      showDialog: false
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    async getEmployees() {
      this.loading = true
      const {
        data: { rows, total }
      } = await reqGetEmployeeList(this.page, this.size)
      this.list = rows
      this.total = total
      this.loading = false
    },
    // size发送变化触发的方法
    handleSizeChange(newVal) {
      this.size = newVal
      this.page = 1
      this.getEmployees()
    },
    // 页码发生变化触发的方法
    handleCurrentChange(newVal) {
      this.page = newVal
      this.getEmployees()
    },
    // 自定义索引列
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    // 格式化聘用形式
    formatter(row, column, cellValue, index) {
      // row 当前行的信息
      // column 是当前列的信息
      // cellValue 单元格的值
      // index 索引
      // console.log(row, column, cellValue, index)
      // return cellValue === 1 ? '正式员工' : '非正式员工'
      // console.log(employeeMenu.hireType)
      const obj = employeeMenu.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 删除员工
    delEmployee(id) {
      // 1. 确认消息提示
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          // 2. 发送请求
          const { message } = await reqDelEmployee(id)
          // 3. 提示用户
          this.$message.success(message)
          // bug page--
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          // 4. 重新渲染
          this.getEmployees()
        })
        .catch(() => {
          this.$message.info('已取消操作')
        })
    }
  }
}
</script>

<style></style>
